/**
 author: chengzheng
 date: 2017/12/26
 email: 13120207061@163.com
 */

const redis = require("./redis");

var logger = require("./logger");

class Producer {
    constructor(option) {

        this.channel = option.channel;
        this.sendCounter = 0;
        this.redisConf = option.dbConf.redis;

    }

    /**
     * return promise object
     * @param tasks
     * @returns {Promise.<void>}
     */
    async sendTaskToQueue(tasks) {
        redis.connect({
            host: this.redisConf.host,
            port: this.redisConf.port,
            password: this.redisConf.password
        });

        for (let task of tasks) {
            await redis.rpush({
                tableName: "queue:" + this.channel,
                value: task
            })
            logger.info(`task send to queue:${++this.sendCounter} ${task.url}`)
        }
    }
}

module.exports = Producer;